<?php
namespace app\admin\controller;

use app\service\MemberCoinService;
use app\service\PayCoinService;
class MemberCoin extends Admin
{
    public function __construct(MemberCoinService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    public function index()
    {
        if (!$this->request->isPost()) {
            $this->assign("title", "钱包列表");
            $payCoinsService = $this->app->make(PayCoinService::class);
            $coinsList = $payCoinsService->getAll([], "id,coin_name,icon","sort asc");
            $this->assign("coinsList", $coinsList);
            return $this->fetch("index");
        }
        $params = $this->request->post();
        $memberId = $params["member_id"] ?? 0;
        $email    = $params["email"] ?? "";
        $coinId   =$params["coin_id"] ?? 0;
        $ret = $this->service->getList([],"*",["member"=>function ($query) use ($memberId, $email) {
            if (!empty($memberId)) {
                $query->where("id", "=", $memberId);
            }
            if (!empty($email)) {
                $query->whereLike("email", "%{$email}%");
            }
        },'coin'=>function ($query) use ($coinId) {
            if (!empty($coinId)) {
                $query->where("id", "=", $coinId);
            }
        }]);
        $this->result($ret);
    }
}